/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.ProductBeanLocal;
import entities.Product;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

/**
 *
 * @author Jesus
 */
@ManagedBean
public class SearchBean {

    @EJB
    private ProductBeanLocal productBean;
    private String input;
    private List<Product> results;
    private Product product;

    public SearchBean() {
    }

    @PostConstruct
    public void init() {
    }

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }

//    public String getParamValue() {
//        return paramValue;
//    }
//
//    public void setParamValue(String paramValue) {
//        this.paramValue = paramValue;
//    }
    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public List<String> complete(String query) {
        List<String> stringResults = new ArrayList<String>();
        results = productBean.findAllLike(query);
        for (Product p : results) {
            stringResults.add(p.getName());
        }
        return stringResults;
    }

    public void goToProduct() throws IOException {
        product = productBean.findByName(input);
        if (product != null) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Tiendalia-war/tiendalia/product.xhtml?product=" + product.getId().toString());
        }

    }
}
